package com.easy.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.common.bean.User;
import com.easy.common.service.IUserService;
import com.easy.common.util.Result;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService userService;
    // 根据id获取信息
    @GetMapping("/id/{id}")
    public Result getById(@PathVariable Integer id){
        return Result.success(userService.getById(id));
    }

    // 根据名称获取信息
    @GetMapping("/username/{username}")
    public Result getByUsername(@PathVariable String username){
        return Result.success(userService.getByUsername(username));
    }

    // 根据手机号获取信息
    @GetMapping("/phone/{phone}")
    public Result getByPhone(@PathVariable String phone){
        return Result.success(userService.getByPhone(phone));
    }

    // 根据邮箱获取信息
    @GetMapping("/email/{email}")
    public Result getByEmail(@PathVariable String email){
        return Result.success(userService.getByEmail(email));
    }

    // 添加用户
    @PostMapping("/addUser")
    public Result addUser(@RequestBody User user){
        return Result.success(userService.addUser(user));
    }

    // 添加管理员
    @PostMapping("/addAdmin")
    public Result addAdmin(@RequestBody User user){
        return Result.success(userService.addAdmin(user));
    }

    // 更新用户
    @PutMapping("/updateUser")
    public Result updateUser(@RequestBody User user){
        return Result.success(userService.updateUser(user));
    }

    // 更新管理员
    @PutMapping("/updateAdmin")
    public Result updateAdmin(@RequestBody User user){
        return Result.success(userService.updateAdmin(user));
    }

    // 删除用户
    @DeleteMapping("/deleteUser/{id}")
    public Result deleteUser(@PathVariable Integer id){
        return Result.success(userService.deleteUser(id));
    }

    // 删除管理员
    @DeleteMapping("/deleteAdmin/{id}")
    public Result deleteAdmin(@PathVariable Integer id){
        return Result.success(userService.deleteAdmin(id));
    }

    // 获取用户列表
    @GetMapping("/userList")
    public Result getUserList(User user){
        if (user.getRole() == null) {
            user.setRole("用户"); // 设置默认角色
        }
        if (user.getRole() != null && user.getRole().equals("用户")){
            return Result.success(userService.getUserAll(user));
        }
        return Result.fail(400, "角色异常");
    }

    // 管理员列表
    @GetMapping("/adminList")
    public Result getAdminList(User user){
        if (user.getRole() == null) {
            user.setRole("管理员"); // 设置默认角色
        }
        if (user.getRole() != null && user.getRole().equals("管理员")){
            return Result.success(userService.getAdminAll(user));
        }
        return Result.fail(400, "角色异常");
    }

    // 获取用户分页列表
    @GetMapping("/userPage")
    public Result getUserPage(User user, Integer current, Integer size){
        if (user.getRole() == null) {
            user.setRole("用户"); // 设置默认角色
        }
        if (user.getRole() != null && user.getRole().equals("用户")){
            IPage<User> page = new Page<>(current, size);
            System.out.println( userService.getUserPage(user,page));
            return Result.success("查询成功",userService.getUserPage(user,page));
        }
        return Result.fail(400,"角色异常");
    }

    // 获取管理员分页列表
    @GetMapping("/adminPage")
    public Result getAdminPage(User user, Integer current, Integer size){
        if (user.getRole() == null) {
            user.setRole("管理员"); // 设置默认角色
        }
        System.out.println(user.getRole());
        if (user.getRole() != null && user.getRole().equals("管理员")){
            IPage<User> page = new Page<>(current, size);
            return Result.success("查询成功",userService.getAdminPage(user,page));
        }
        return Result.fail(400,"角色异常");
    }

    // 重置密码(用户自己修改)
    @PutMapping("/resetPassword")
    public Result resetPassword(@RequestBody Map<String, String> passwordData) {
        try {
            boolean result = userService.resetPassword(
                    passwordData.get("username"),
                    passwordData.get("oldPassword"),
                    passwordData.get("newPassword")
            );
            if (result) {
                return Result.success(200,"密码重置成功", "OK");
            } else {
                return Result.fail(500, "原密码错误", null);
            }
        } catch (Exception e) {
            return Result.fail(500, e.getMessage(), null);
        }
    }
}
